variable "launch_time" {
  type        = string
  nullable    = false
  description = <<EOT
  {
    "Description": {
      "en": "Time point at which the scheduled task is triggered.\nThe date format follows the ISO8601 standard and uses UTC time. It is in the format of YYYY-MM-DDThh:mmZ.\nIf RecurrenceType is specified, the time point specified by this attribute is the default time point at which the circle is executed. If RecurrenceType is not specified, the task is executed once on the designated date and time.\nA time point 90 days after creation or modification cannot be entered."
    },
    "AllowedPattern": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}[:]\\d{2}Z$",
    "Label": {
      "en": "LaunchTime",
      "zh-cn": "定时任务触发的时间点"
    }
  }
  EOT
}

variable "description" {
  type        = string
  description = <<EOT
  {
    "AssociationProperty": "TextArea",
    "Description": {
      "en": "Description of the scheduled task, which is 2-200 characters (English or Chinese) long."
    },
    "Label": {
      "en": "Description",
      "zh-cn": "定时任务的描述信息"
    },
    "MinLength": 2,
    "MaxLength": 200
  }
  EOT
}

variable "scaling_group_id" {
  type        = string
  description = <<EOT
  {
    "Description": {
      "en": "The globally unique ID of the scheduled task. The globally unique ID is generated by the system.\n**Note**: You cannot specify the **ScheduledAction** and **ScalingGroupId** parameters at the same time.",
      "zh-cn": "定时任务触发时需要修改实例数量的伸缩组，填写伸缩组ID。"
    },
    "Label": {
      "en": "ScalingGroupId",
      "zh-cn": "定时任务触发时需要修改实例数量的伸缩组"
    }
  }
  EOT
}

variable "recurrence_type" {
  type        = string
  description = <<EOT
  {
    "Description": {
      "en": "Type of the scheduled task to be repeated. Optional values:\n- Daily: Recurrence interval by day for a scheduled task.\n- Weekly: Recurrence interval by week for a scheduled task.\n- Monthly: Recurrence interval by month for a scheduled task.\nRecurrenceType, RecurrenceValue and RecurrenceEndTime must be specified."
    },
    "AllowedValues": [
      "Daily",
      "Weekly",
      "Monthly"
    ],
    "Label": {
      "en": "RecurrenceType",
      "zh-cn": "重复执行定时任务的类型"
    }
  }
  EOT
}

variable "desired_capacity" {
  type        = number
  description = <<EOT
  {
    "Description": {
      "en": "The ID of the scaling group whose number of instances must be modified when the scheduled task is triggered. If you specify the **ScalingGroupId** parameter for a scheduled task, you must specify the minimum, maximum, or expected numbers of instances for a scaling group in the scheduled task. That is, you must specify at least one of the **MinValue**, **MaxValue**, and **DesiredCapacity** parameters.",
      "zh-cn": "定时任务的伸缩方式为设置伸缩组内实例数量时，指定伸缩组内实例的期望实例数。"
    },
    "Label": {
      "en": "DesiredCapacity",
      "zh-cn": "定时任务的伸缩方式为设置伸缩组内实例数量时"
    }
  }
  EOT
}

variable "scheduled_task_name" {
  type        = string
  description = <<EOT
  {
    "Description": {
      "en": "Display name of the scheduled task, which must be 2-40 characters (English or Chinese) long. It must begin with a number, an upper/lower-case letter or a Chinese character and may contain numbers, \"_\", \"-\" or \".\".\nThe account name is unique in the same region.\nIf this parameter is not specified, the default value ScheduledScalingTaskId is used."
    },
    "AllowedPattern": "^[a-zA-Z0-9\\u4e00-\\u9fa5][-_.a-zA-Z0-9\\u4e00-\\u9fa5]{1,63}$",
    "Label": {
      "en": "ScheduledTaskName",
      "zh-cn": "定时任务的显示名称"
    }
  }
  EOT
}

variable "max_value" {
  type        = number
  description = <<EOT
  {
    "Description": {
      "en": "The expected number of instances in the scaling group if you specify the ScalingGroupId parameter.\n**Note**: You must specify the **DesiredCapacity** parameter when you create a scaling group.",
      "zh-cn": "定时任务的伸缩方式为设置伸缩组内实例数量时，指定伸缩组内实例的最大数量。"
    },
    "Label": {
      "en": "MaxValue",
      "zh-cn": "定时任务的伸缩方式为设置伸缩组内实例数量时"
    }
  }
  EOT
}

variable "launch_expiration_time" {
  type        = number
  description = <<EOT
  {
    "Description": {
      "en": "Time period within which the failed scheduled task is retried.\nThe default value is 600s.\nValue range: [0, 21600]",
      "zh-cn": "定时任务触发操作失败后，在此时间内重试。"
    },
    "MinValue": 0,
    "Label": {
      "en": "LaunchExpirationTime",
      "zh-cn": "定时任务触发操作失败后"
    },
    "MaxValue": 21600
  }
  EOT
}

variable "min_value" {
  type        = number
  description = <<EOT
  {
    "Description": {
      "en": "The maximum number of instances in the scaling group if you specify the ScalingGroupId parameter.",
      "zh-cn": "定时任务的伸缩方式为设置伸缩组内实例数量时，指定伸缩组内实例的最小数量。"
    },
    "Label": {
      "en": "MinValue",
      "zh-cn": "定时任务的伸缩方式为设置伸缩组内实例数量时"
    }
  }
  EOT
}

variable "scheduled_action" {
  type        = string
  description = <<EOT
  {
    "Description": {
      "en": "Operations performed when the scheduled task is triggered. Fill in the unique identifier of the scaling rule.\n**Note**: You cannot specify the **ScheduledAction** and **ScalingGroupId** parameters at the same time."
    },
    "Label": {
      "en": "ScheduledAction",
      "zh-cn": "定时任务触发时需要执行的操作"
    },
    "MaxLength": 200
  }
  EOT
}

variable "recurrence_end_time" {
  type        = string
  description = <<EOT
  {
    "Description": {
      "en": "End time of the scheduled task to be repeated.\nThe date format follows the ISO8601 standard and uses UTC time. It is in the format of YYYY-MM-DDThh:mmZ.\nA time point 90 days after creation or modification cannot be entered.\nRecurrenceType, RecurrenceValue and RecurrenceEndTime must be specified."
    },
    "AllowedPattern": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}[:]\\d{2}Z$",
    "Label": {
      "en": "RecurrenceEndTime",
      "zh-cn": "重复执行定时任务的结束时间"
    }
  }
  EOT
}

variable "recurrence_value" {
  type        = string
  description = <<EOT
  {
    "Description": {
      "en": "Value of the scheduled task to be repeated.\n- Daily: Only one value in the range [1,31] can be filled.\n- Weekly: Multiple values can be filled. The values of Sunday to Saturday are 0 to 6 in sequence. Multiple values shall be separated by a comma \",\".\n- Monthly: In the format of A-B. The value range of A and B is 1 to 31, and the B value must be greater than the A value.\nRecurrenceType, RecurrenceValue and RecurrenceEndTime must be specified."
    },
    "AllowedPattern": "^(\\d{1,2})$|^([0-6](,\\s*[0-6]){,6})$|^(\\d{1,2}[-]\\d{1,2})$",
    "Label": {
      "en": "RecurrenceValue",
      "zh-cn": "重复执行定时任务的数值"
    },
    "MaxLength": 50
  }
  EOT
}

variable "task_enabled" {
  type        = bool
  description = <<EOT
  {
    "Description": {
      "en": "Whether to enable the scheduled task.\n- When the parameter is set to true, the task is enabled.\n- When the parameter is set to false, the task is disabled.\nThe default value is true."
    },
    "Label": {
      "en": "TaskEnabled",
      "zh-cn": "是否启动定时任务"
    }
  }
  EOT
}

resource "alicloud_ess_scheduled_task" "scheduled_task" {
  launch_time            = var.launch_time
  description            = var.description
  scaling_group_id       = var.scaling_group_id
  recurrence_type        = var.recurrence_type
  desired_capacity       = var.desired_capacity
  scheduled_task_name    = var.scheduled_task_name
  max_value              = var.max_value
  launch_expiration_time = var.launch_expiration_time
  min_value              = var.min_value
  scheduled_action       = var.scheduled_action
  recurrence_end_time    = var.recurrence_end_time
  recurrence_value       = var.recurrence_value
  task_enabled           = var.task_enabled
}

output "scheduled_task_id" {
  value       = alicloud_ess_scheduled_task.scheduled_task.id
  description = "ID of the scheduled task, which is generated by the system and globally unique."
}

